<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
/**
 * 员工卡/副卡/会员卡操作表
 * */
class OptLog extends AdminBase{
	protected $beforeActionList=[
			'before_index'=>['only'=>'index'],
	];
	function _initialize(){
		parent::_initialize();	
	}
	function index(){
		echo $this->fetch();
	}
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	function detail(){
		if(empty(input("id"))){
			var_dump("参数错误");die;
		}
		$model=db("OptLog")->find(input("id"));
		$old_data=json_decode($model['old_data'],TRUE);
		$new_data=json_decode($model['new_data'],TRUE);
		if(empty($old_data)){
			$old_data=array('card_no'=>'','in_varifycation'=>'','in_varifycation_type'=>'','start_date'=>'','end_date'=>'','mobile'=>'','name'=>'','owner_type'=>'','icon1'=>'');
		}
		if(empty($new_data)){
			$new_data=array('card_no'=>'','in_varifycation'=>'','in_varifycation_type'=>'','start_date'=>'','end_date'=>'','mobile'=>'','name'=>'','owner_type'=>'','icon1'=>'');
		}
		$this->assign("old_data",$old_data);
		$this->assign("new_data",$new_data);
		if(isset($old_data['icon1'])&&!empty($old_data['icon1'])){
			$this->assign("icons",explode(",",$old_data['icon1']));
		}else{
			$this->assign("icons","");
		}
		if(isset($new_data['icon1'])&&!empty($new_data['icon1'])){
			$this->assign("icons1",explode(",",$new_data['icon1']));
		}else{
			$this->assign("icons1","");
		}
		echo $this->fetch();
	}
	//////////////////////////////////////异步调用api//////////////////////////////////////
	/**
	 * 新增操作日志
	 * */
	function api_add($member,$icon){
		if(empty($member)){
			return;
		}else{
			$member=$this->filtermember($member);
			$member['icon1']=$icon;
			$data['name']=$member['name'];
			$data['mobile']=$member['mobile'];
			$data['in_varifycation_type']=$member['in_varifycation_type'];
			$data['in_varifycation']=$member['in_varifycation'];
			$data['card_no']=$member['card_no'];
			$data['owner_type']=$member['owner_type'];
			$data['admin_id']=sp_get_current_admin_id();
			$data['admin_account']=sp_get_current_admin()['account'];
			$data['admin_mobile']=sp_get_current_admin()['mobile'];
			$data['shop_id']=sp_get_current_shop_id();
			$data['opt_type']=1;
			$data['new_data']=json_encode($member,JSON_UNESCAPED_UNICODE);
			$data['old_data']='{}';
			db("OptLog")->insert($data);
		}
	}
	/**
	 * 修改操作日志
	 * */
	function api_edit($member,$icon,$member1,$icon1){
		if(empty($member)){
			return;
		}else{
			$member=$this->filtermember($member);
			$member1=$this->filtermember($member1);
			$member1['icon1']=$icon1;
			$member['icon1']=$icon;
			$data['name']=$member1['name'];
			$data['mobile']=$member1['mobile'];
			$data['in_varifycation_type']=$member1['in_varifycation_type'];
			$data['in_varifycation']=$member1['in_varifycation'];
			$data['card_no']=$member1['card_no'];
			$data['owner_type']=$member1['owner_type'];
			$data['admin_id']=sp_get_current_admin_id();
			$data['admin_account']=sp_get_current_admin()['account'];
			$data['admin_mobile']=sp_get_current_admin()['mobile'];
			$data['shop_id']=sp_get_current_shop_id();
			$data['opt_type']=2;
			$data['new_data']=json_encode($member1,JSON_UNESCAPED_UNICODE);
			$data['old_data']=json_encode($member,JSON_UNESCAPED_UNICODE);;
			db("OptLog")->insert($data);
		}
	}
	
	/**
	 * 删除操作日志
	 * */
	function api_del($member,$icon){
		if(empty($member)){
			return;
		}else{
			$member=$this->filtermember($member);
			$member['icon1']=$icon;
			$data['name']=$member['name'];
			$data['mobile']=$member['mobile'];
			$data['in_varifycation_type']=$member['in_varifycation_type'];
			$data['in_varifycation']=$member['in_varifycation'];
			$data['card_no']=$member['card_no'];
			$data['owner_type']=$member['owner_type'];
			$data['admin_id']=sp_get_current_admin_id();
			$data['admin_account']=sp_get_current_admin()['account'];
			$data['admin_mobile']=sp_get_current_admin()['mobile'];
			$data['shop_id']=sp_get_current_shop_id();
			$data['opt_type']=3;
			$data['old_data']=json_encode($member,JSON_UNESCAPED_UNICODE);
			$data['new_data']='{}';
			db("OptLog")->insert($data);
		}
		return;
	}
	/////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10041025");
		$this->assign("menus",$auth_group);
	}
	///////////////////////////////////////////////////
	function  _ajax_index(){
		$query=[
				'name'=>['field'=>'name','operator'=>'='],
				'card_no'=>['field'=>'card_no','operator'=>'='],
				'admin_account'=>['field'=>'admin_account','operator'=>'='],
				'opt_type'=>['field'=>'opt_type','operator'=>'='],
		];
		$where_and=join(" and ",sp_get_param_sql(request()->isPost, $query));
		$shop_id=sp_get_current_shop_id();
		$where_and.=" and shop_id=$shop_id";
		$total=db("OptLog")->where($where_and)->fetchsql(FALSE)->count(1);
		$field='id,card_no,name,mobile,owner_type,in_varifycation,in_varifycation_type,admin_account,opt_type,create_time';
		$list=db("OptLog")->where($where_and)->order($this->get_order())->limit($this->get_limit())->field($field)->select();
		return ['pageData'=>$list,'total'=>$total];
	}
	/**
	 * filtermember 过滤会员
	 * */
	function filtermember($member){
		if(isset($member['fingerprint'])){
			unset($member['fingerprint']);
		}
		if(isset($member['sliding_group_id'])){
			unset($member['sliding_group_id']);
		}
		if(isset($member['descs'])){
			unset($member['descs']);
		}
		if(isset($member['doors_auth'])){
			unset($member['doors_auth']);
		}
		return $member;
	}
	
}
